################################################################################
##                                                                            ##
##  Title         : Standards of Good Practice and the Methodology of         ##
##                  Necessary Conditions in Qualitative Comparative Analysis  ##
##  Author        : Alrik Thiem (alrik.thiem@unige.ch)                        ##
##  Date          : 23 July 2016                                              ##
##                                                                            ##
##  R version     : >=3.2.5                                                   ##
##  QCApro version: 1.1-1                                                     ##
################################################################################

#-------------------------------------------------------------------------------
# unzip folder with datasets in replication package, then load datasets into
# list (adjust file path if necessary) and name them (all datasets were obtained
# from http://www.compasss.org/bibdata.htm, accessed 26 April 2016)
#-------------------------------------------------------------------------------

studies <- paste0(c("Avdagic2010", "BankEtal2015", "BasedauRichter2014",
   "CebotariVink2013", "DaRoitWeicht2013", "DavidssonEmmenegger2013",
   "Emmenegger2011", "EppleEtal2014", "Fischer2015", "HamidovEtal2015",
   "IshiyamaBatta2012", "Karlas2012", "Lilliefeldt2012", "Mello2012",
   "Metelits2009", "PahlWostlKnieper2014", "Palm2013", "ParkYoung2015",
   "SchneiderMakszin2014", "Thomann2015", "VerweijEtal2013"), "set.csv")

dat <- vector("list", length(studies))
dat <- lapply(studies, read.csv, row.names = 1)
names(dat) <- c("AVD","BAN","BAS","CEB","DAR","DAV","EMM","EPP","FIS","HAM","ISH",
                "KAR","LIL","MEL","MET","PAH","PAL","PAR","SCH","THO","VER")

#-------------------------------------------------------------------------------
# The CONSOL-Effect
#-------------------------------------------------------------------------------

# data from Ragin, Charles C. 2009. "Qualitative Comparative Analysis Using Fuzzy
# Sets (fsQCA)." In Configurational Comparative Methods: Qualitative Comparative
# Analysis (QCA) and Related Techniques, ed. B. Rihoux and C. C. Ragin. London:
# Sage Publications, p.95.

LIP <- data.frame(
 D = c(0.81,0.99,0.58,0.16,0.58,0.98,0.89,0.04,0.07,
       0.72,0.34,0.98,0.02,0.01,0.01,0.03,0.95,0.98),
 U = c(0.12,0.89,0.98,0.07,0.03,0.03,0.79,0.09,0.16,
       0.05,0.10,1.00,0.17,0.02,0.03,0.30,0.13,0.99),
 L = c(0.99,0.98,0.98,0.98,0.99,0.99,0.99,0.13,0.88,
       0.98,0.41,0.99,0.59,0.01,0.17,0.09,0.99,0.99),
 I = c(0.73,1.00,0.90,0.01,0.08,0.81,0.96,0.36,0.07,
       0.01,0.47,0.94,0.00,0.11,0.00,0.21,0.67,1.00),
 G = c(0.43,0.98,0.91,0.91,0.58,0.95,0.31,0.43,0.13,
       0.95,0.58,0.99,0.00,0.01,0.84,0.20,0.91,0.98),
 S = c(0.05,0.95,0.89,0.12,0.77,0.95,0.05,0.06,0.42,
       0.92,0.05,0.95,0.12,0.05,0.21,0.06,0.95,0.95)
)

rownames(LIP) <- c("AT","BE","CZ","EE","FI","FR","DE","GR","HU",
                   "IE","IT","NL","PL","PT","RO","ES","SE","UK")

# load QCApro package
library(QCApro)

# truth table (Table 1; note that rows in the truth table produced by truthTable
# are in different order from Table 1 presented in the article; rows 1-9 in
# Table 1 are the same as those presented in Schneider and Wagemann (2013, 213,
# Table 1); remainders are in the same order)
tt <- truthTable(LIP, outcome = "S", incl.cut1 = 0.8, show.cases = TRUE,
                 complete = TRUE)
tt

# model m1 in Expression (1)
eQMC(tt)

# Table 2
round(superSubset(LIP, outcome = "S", incl.cut = 0.9)$incl.cov[,-2], 3)

# remainders barred from use by QMC in minimization based only on �L and �G
omit.TESA <- rownames(tt$tt[tt$tt$OUT == "?" & (tt$tt$L == 0 | tt$tt$G == 0), ])
eQMC(tt, omit = omit.TESA)

# remainders barred from use by QMC in minimization based on all minimal
# disjunctions of simple conditions
omit <- rownames(tt$tt[tt$tt$OUT == "?" & (
  (tt$tt$L == 0 | tt$tt$G == 0) | # �L + �G
  (tt$tt$I == 0 & tt$tt$U == 1) | # �I *  U
  (tt$tt$D == 0 & tt$tt$G == 1) | # �D *  G
  (tt$tt$D == 0 & tt$tt$I == 1) | # �D *  I
  (tt$tt$D == 0 & tt$tt$U == 1) | # �D *  U
  (tt$tt$D == 0 & tt$tt$L == 1 & tt$tt$I == 0) | # �D *  L * �I
  (tt$tt$D == 0 & tt$tt$U == 0 & tt$tt$I == 0) | # �D * �U * �I
  (tt$tt$D == 0 & tt$tt$U == 0 & tt$tt$L == 1)   # �D * �U *  L
),])

# corresponding solution; same as conservative solution because no remainder
# is available to QMC
eQMC(tt, omit = omit)

# share of remainders eliminated by T/ESA
testTESA(LIP, exo.facs = c("D","U","L","I","G"), outcome = "S", incl.cut1 = 0.8)

#-------------------------------------------------------------------------------
# The Prevalence of the CONSOL-Effect in Applied Research: A Meta-Analysis
#-------------------------------------------------------------------------------

fctrsAVD <- c("MAAS", "UNEM", "DENS", "CENT", "MEDC", "LEFT", "MING", "EFFP")
fctrsDAR <- c("EXPSERV", "PRIV", "EXPCASHFREE", "WORKMIG", "UNDOCMIG",
            "LOWSKILLMIG", "UNDERECON")
fctrsPAH <- c("COR", "DIS", "LEG", "CPI", "GDP")

# 1. Avdagic (2010)
rownames(superSubset(dat$AVD, exo.facs = fctrsAVD[c(1,2,5,7)], outcome = "SOCP", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$AVD, exo.facs = fctrsAVD[c(1,5,7,8)], outcome = "SOCP", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$AVD, exo.facs = fctrsAVD[c(1,2,5,7,8)], outcome = "SOCP", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$AVD, exo.facs = fctrsAVD[c(1,2,5,7)], outcome = "SOCP", incl.cut = 0.75)$incl.cov)
testTESA(dat$AVD, exo.facs = fctrsAVD[c(1,2,5,7)], outcome = "SOCP", incl.cut1 = 0.75)
rownames(superSubset(dat$AVD, exo.facs = fctrsAVD[c(1,5,7,8)], outcome = "SOCP", incl.cut = 0.75)$incl.cov)
testTESA(dat$AVD, exo.facs = fctrsAVD[c(1,5,7,8)], outcome = "SOCP", incl.cut1 = 0.75)
rownames(superSubset(dat$AVD, exo.facs = fctrsAVD[c(1,2,5,7,8)], outcome = "SOCP", incl.cut = 0.75)$incl.cov)
testTESA(dat$AVD, exo.facs = fctrsAVD[c(1,2,5,7,8)], outcome = "SOCP", incl.cut1 = 0.75)

# 2. Bank, Richter, and Sunik (2015)
rownames(superSubset(dat$BAN, outcome = "Y", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$BAN, outcome = "Y", incl.cut = 0.75)$incl.cov)
testTESA(dat$BAN, exo.facs = c("A", "B", "C", "D", "E"), outcome = "Y", incl.cut1 = 0.75)
rownames(superSubset(dat$BAN, outcome = "Y", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$BAN, outcome = "Y", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$BAN, exo.facs = c("A", "B", "C", "D", "E"), outcome = "Y", neg.out = TRUE, incl.cut1 = 0.75)

# 3. Basedau and Richter (2014)
rownames(superSubset(dat$BAS, outcome = "CWO", incl.cut = 1)$incl.cov)
rownames(superSubset(dat$BAS, outcome = "CWO", incl.cut = 0.75)$incl.cov)
testTESA(dat$BAS, outcome = "CWO", incl.cut1 = 0.75)
rownames(superSubset(dat$BAS, outcome = "CWO", neg.out = TRUE, incl.cut = 1)$incl.cov)
rownames(superSubset(dat$BAS, outcome = "CWO", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$BAS, outcome = "CWO", neg.out = TRUE, incl.cut1 = 0.75)

# 4. Cebotari and Vink (2013)
rownames(superSubset(dat$CEB, outcome = "PROTEST", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$CEB, outcome = "PROTEST", incl.cut = 0.75)$incl.cov)
testTESA(dat$CEB, outcome = "PROTEST", incl.cut1 = 0.75)
rownames(superSubset(dat$CEB, outcome = "PROTEST", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$CEB, outcome = "PROTEST", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$CEB, outcome = "PROTEST", neg.out = TRUE, incl.cut1 = 0.75)

# 5. Da Roit and Weicht (2013)
rownames(superSubset(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFAM", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFAM", incl.cut = 0.75)$incl.cov)
testTESA(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFAM", incl.cut1 = 0.75)
rownames(superSubset(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFOR", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFOR", incl.cut = 0.75)$incl.cov)
testTESA(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFOR", incl.cut1 = 0.75)
rownames(superSubset(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFAM", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFAM", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFAM", neg.out = TRUE, incl.cut1 = 0.75)
rownames(superSubset(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFOR", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFOR", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$DAR, exo.facs = fctrsDAR, outcome = "MIGFOR", neg.out = TRUE, incl.cut1 = 0.75)

# 6. Davidsson and Emmenegger (2013)
rownames(superSubset(dat$DAV, outcome = "TWOTIER", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$DAV, outcome = "TWOTIER", incl.cut = 0.75)$incl.cov)
testTESA(dat$DAV, outcome = "TWOTIER", incl.cut1 = 0.75)

# 7. Emmenegger (2011)
rownames(superSubset(dat$EMM, outcome = "JSR", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$EMM, outcome = "JSR", incl.cut = 0.75)$incl.cov)
testTESA(dat$EMM, outcome = "JSR", incl.cut1 = 0.75)

# 8. Epple, Gasser, Kersten, Nollert and Schief (2014)
rownames(superSubset(dat$EPP, outcome = "D", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$EPP, outcome = "D", incl.cut = 0.75)$incl.cov)
testTESA(dat$EPP, outcome = "D", incl.cut1 = 0.75)
rownames(superSubset(dat$EPP, outcome = "D", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$EPP, outcome = "D", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$EPP, outcome = "D", neg.out = TRUE, incl.cut1 = 0.75)

# 9. Fischer (2015)
rownames(superSubset(dat$FIS, outcome = "DOM", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$FIS, outcome = "DOM", incl.cut = 0.75)$incl.cov)
testTESA(dat$FIS, outcome = "DOM", incl.cut1 = 0.75)

# 10. Hamidov, Thiel and Zikos (2015)
rownames(superSubset(dat$HAM, outcome = "MIC", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$HAM, outcome = "MIC", incl.cut = 0.75)$incl.cov)
testTESA(dat$HAM, outcome = "MIC", incl.cut1 = 0.75)

# 11. Ishiyama and Batta (2012)
rownames(superSubset(dat$ISH, outcome = "DPS", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$ISH, outcome = "DPS", incl.cut = 0.75)$incl.cov)
testTESA(dat$ISH, outcome = "DPS", incl.cut1 = 0.75)
rownames(superSubset(dat$ISH, outcome = "DPS", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$ISH, outcome = "DPS", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$ISH, outcome = "DPS", neg.out = TRUE, incl.cut1 = 0.75)

# 12. Karlas (2012)
rownames(superSubset(dat$KAR, outcome = "CONT", incl.cut = 0.8)$incl.cov)
rownames(superSubset(dat$KAR, outcome = "CONT", incl.cut = 0.75)$incl.cov)
testTESA(dat$KAR, outcome = "CONT", incl.cut1 = 0.75)

# 13. Lilliefeldt (2012)
rownames(superSubset(dat$LIL, outcome = "BALANCE", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$LIL, outcome = "BALANCE", incl.cut = 0.75)$incl.cov)
testTESA(dat$LIL, outcome = "BALANCE", incl.cut1 = 0.75)
rownames(superSubset(dat$LIL, outcome = "BALANCE", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$LIL, outcome = "BALANCE", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$LIL, outcome = "BALANCE", neg.out = TRUE, incl.cut1 = 0.75)

# 14. Mello (2012)
rownames(superSubset(dat$MEL, outcome = "MP", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$MEL, outcome = "MP", incl.cut = 0.75)$incl.cov)
testTESA(dat$MEL, outcome = "MP", incl.cut1 = 0.75)
rownames(superSubset(dat$MEL, outcome = "MP", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$MEL, outcome = "MP", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$MEL, outcome = "MP", neg.out = TRUE, incl.cut1 = 0.75)

# 15. Metelits (2009)
rownames(superSubset(dat$MET, outcome = "COERCE", incl.cut = 0.8)$incl.cov)
rownames(superSubset(dat$MET, outcome = "COERCE", incl.cut = 0.75)$incl.cov)
testTESA(dat$MET, outcome = "COERCE", incl.cut1 = 0.75)

# 16. Pahl-Wostl and Knieper (2014)
rownames(superSubset(dat$PAH, exo.facs = fctrsPAH, outcome = "ADAP", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$PAH, exo.facs = fctrsPAH, outcome = "ADAP", incl.cut = 0.75)$incl.cov)
testTESA(dat$PAH, exo.facs = fctrsPAH, outcome = "ADAP", incl.cut1 = 0.75)
rownames(superSubset(dat$PAH, exo.facs = fctrsPAH, outcome = "ADAP", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$PAH, exo.facs = fctrsPAH, outcome = "ADAP", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$PAH, exo.facs = fctrsPAH, outcome = "ADAP", neg.out = TRUE, incl.cut1 = 0.75)

# 17. Palm (2013)
rownames(superSubset(dat$PAL, outcome = "IWS", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$PAL, outcome = "IWS", incl.cut = 0.75)$incl.cov)
testTESA(dat$PAL, outcome = "IWS", incl.cut1 = 0.75)
rownames(superSubset(dat$PAL, outcome = "IWS", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$PAL, outcome = "IWS", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$PAL, outcome = "IWS", neg.out = TRUE, incl.cut1 = 0.75)

# 18. Park and Young (2015)
rownames(superSubset(dat$PAR, outcome = "WM", incl.cut = 0.95)$incl.cov)
rownames(superSubset(dat$PAR, outcome = "WM", incl.cut = 0.75)$incl.cov)
testTESA(dat$PAR, outcome = "WM", incl.cut1 = 0.75)

# 19. Schneider and Makszin (2014)
rownames(superSubset(dat$SCH, outcome = "LPI", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$SCH, outcome = "LPI", incl.cut = 0.75)$incl.cov)
testTESA(dat$SCH, outcome = "LPI", incl.cut1 = 0.75)

# 20. Thomann (2015)
rownames(superSubset(dat$THO, outcome = "PERF", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$THO, outcome = "PERF", incl.cut = 0.75)$incl.cov)
testTESA(dat$THO, outcome = "PERF", incl.cut1 = 0.75)
rownames(superSubset(dat$THO, outcome = "PERF", neg.out = TRUE, incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$THO, outcome = "PERF", neg.out = TRUE, incl.cut = 0.75)$incl.cov)
testTESA(dat$THO, outcome = "PERF", neg.out = TRUE, incl.cut1 = 0.75)

# 21. Verweij, Klijn, Edelenbos and Van Buuren (2013)
rownames(superSubset(dat$VER, outcome = "O", incl.cut = 0.9)$incl.cov)
rownames(superSubset(dat$VER, outcome = "O", incl.cut = 0.75)$incl.cov)
testTESA(dat$VER, outcome = "O", incl.cut1 = 0.9)

# References for meta analysis

# 1.  Avdagic, Sabina. 2010. When are concerted reforms feasible? Explaining the
#       emergence of social pacts in Western Europe. Comparative Political
#       Studies 43(5):628-57.
# 2.  Bank, Andr�, Thomas Richter, and Anna Sunik. 2015. Long-term monarchical
#       survival in the Middle East: A configurational comparison, 1945-2012.
#       Democratization 22(1):179-200.
# 3.  Basedau, Matthias, and Thomas Richter. 2014. Why do some oil exporters
#       experience civil war but others do not? Investigating the conditional
#       effects of oil. European Political Science Review 6(4):549-74.
# 4.  Cebotari, Victor, and Maarten P. Vink. 2013. A configurational analysis of
#       ethnic protest in Europe. International Journal of Comparative Sociology
#       54(4):298-324.
# 5.  Da Roit, Barbara, and Bernhard Weicht. 2013. Migrant care work and care,
#       migration and employment regimes: A fuzzy-set analysis. Journal of
#       European Social Policy 23(5):469-486.
# 6.  Davidsson, Johan B., and Patrick Emmenegger. 2013. Defending the
#       organisation, not the members: Unions and the reform of job security
#       legislation in Western Europe. European Journal of Political Research
#       52(3):339-63.
# 7.  Emmenegger, Patrick. 2011. Job security regulations in Western Democracies:
#       A fuzzy set analysis. European Journal of Political Research 50(3):336-64.
# 8.  Epple, Ruedi, Martin Gasser, Sarah Kersten, Michael Nollert, and Sebastian
#       Schief. 2014. Institutions and gender time inequality: A fuzzy-set QCA
#       of Swiss cantons. Swiss Journal of Sociology 40(2):259-78.
# 9.  Fischer, Manuel. 2015. Institutions and coalitions in policy processes: A
#       cross-sectoral comparison. Journal of Public Policy 35(2):245-68.
# 10. Hamidov, Ahmad, Andreas Thiel, and Dimitrios Zikos. 2015. Institutional
#       design in transformation: A comparative study of local irrigation
#       governance in Uzbekistan.� Environmental Science & Policy 53(0):175-91.
# 11. Ishiyama, John, and Anna Batta. 2012. The emergence of dominant political
#       party systems in unrecognized states. Communist and Post-Communist Studies
#       45(1-2):123-30.
# 12. Karlas, Jan. 2012. National parliamentary control of EU affairs:
#       Institutional design after enlargement. West European Politics
#       35(5):1095-113.
# 13. Lilliefeldt, Emelie. 2012. �Party and gender in Western Europe revisited:
#       A fuzzy-set Qualitative Comparative Analysis of gender-balanced parliamentary
#       parties. Party Politics 18(2):193-214.
# 14. Mello, Patrick A. 2012. Parliamentary peace or partisan politics? Democracies�
#       participation in the Iraq War. Journal of International Relations and
#       Development 15(3):420-53.
# 15. Metelits, Claire M. 2009. The consequences of rivalry: Explaining insurgent
#       violence using fuzzy sets. Political Research Quarterly 62(4):673-84.
# 16. Pahl-Wostl, Claudia, and Christian Knieper. 2014. The capacity of water
#       governance to deal with the climate change adaptation challenge: Using
#       fuzzy set Qualitative Comparative Analysis to distinguish between polycentric,
#       fragmented and centralized regimes. Global Environmental Change 29:139-54.
# 17. Palm, Trineke. 2013. Embedded in social cleavages: An explanation of the
#       variation in timing of women�s suffrage. Scandinavian Political Studies
#       36(1):1-22.
# 18. Park, Sung H., and Kevin L. Young. 2015. Wage moderation in the public
#       sector: The experiences of 11 EMU countries in the recent economic crisis,
#       2008�2010. Economic and Industrial Democracy 36(4):575-609.
# 19. Schneider, Carsten Q., and Kristin Makszin. 2014. Forms of welfare capitalism
#       and education-based participatory inequality. Socio-Economic Review
#       12(2):437-62.
# 20. Thomann, Eva. 2015. Is output performance all about the resources? A fuzzy-set
#       Qualitative Comparative Analysis of street-level bureaucrats in Switzerland.
#       Public Administration 93 (1):177-94.
# 21. Verweij, Stefan, Erik-Hans Klijn, Jurian Edelenbos, and Arwin Van Buuren.
#       2013. What makes governance networks work? A fuzzy set Qualitative
#       Comparative Analysis of 14 Dutch spatial planning projects. Public
#       Administration 91(4):1035-55.